package com.wmh.baseservice.merchant.controller;

import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.user.pojo.UsEvaluate;
import com.wmh.baseservice.user.pojo.UsInfo;
import com.wmh.baseservice.user.service.UsEvaluateService;
import com.wmh.baseservice.user.service.UsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "商家端-评价相关接口")
@RestController
@RequestMapping(value = "merchant/evaluate", produces = "text/plain;charset=utf-8")
@RequiresRoles("merchant")
public class MerchantEvaluateController {

    @Resource
    private UsEvaluateService usEvaluateService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private UsInfoService usInfoService;


    @ApiOperation("评价列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String list(PageInfo pageInfo) {
        Long id = (Long) request.getAttribute("id");
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort("create_time", SortWay.DESC);
        //执行分页辅助工具
        PageResult<UsEvaluate> pageResult = new PageUtil<UsEvaluate>(pageInfo).startPage((page, queryWrapper) -> {
            queryWrapper.eq(UsEvaluate.MERCHANT_ID, id);
            usEvaluateService.page(page, queryWrapper);
        });
        pageResult.getList().forEach(item->{
            UsInfo usInfo = usInfoService.getByIdForUpdate(item.getUsId());
            if (CommUtil.checkNull(usInfo)){
                item.setRemark(usInfo.getNick());
            }
        });
        return ReturnBody.success(pageResult);
    }
}
